﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ITKRestaurant.RestaurantServiceReference;
using MyWCFServices.RealRestaurantService;
using System.Data;

namespace ITKRestaurant
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class RendelesGUI : Window
    {
        RestaurantServiceClient client = null;
        List<FogyasztasiCikk> cikkek = null;
        List<FogyasztasiCikk> rendelt = new List<FogyasztasiCikk>();

        Asztal asztal = null;

        EtteremGUI etterem = null;

        public RendelesGUI(RestaurantServiceClient c, Asztal a, EtteremGUI e)
        {
            this.client = c;
            this.asztal = a;
            this.etterem = e;
            InitializeComponent();
            initEtlap();
        }


        public void initEtlap()
        {
            cikkek = new List<FogyasztasiCikk>();
            cikkek = client.GetAllFogyasztasiCikkek().ToList();
            var kajak = from i in cikkek
                        where i.FogyasztasiCikkTipus == FogyasztasiCikkTipus.Etel
                        orderby i.FogyasztasiCikkNev
                        select i;

            this.dataGrid1.Columns.Clear();
            this.dataGrid1.Items.Clear();

            this.dataGrid1.IsReadOnly = true;

            foreach(FogyasztasiCikk kaja in kajak)
            {
                    this.dataGrid1.Items.Add(new tetelek() { tetel = kaja.FogyasztasiCikkNev, ar = "" + kaja.FogyasztasiCikkAr.ToString() + " Ft" });
            }

            DataGridTextColumn col1 = new DataGridTextColumn();
            col1.Binding = new Binding("tetel");

            DataGridTextColumn col2 = new DataGridTextColumn();
            col2.Binding = new Binding("ar");

            dataGrid1.Columns.Add(col1);
            dataGrid1.Columns.Add(col2);

            col1.Header = "Tétel";
            col1.Width = 200;
            col2.Header = "Ár";
            col2.Width = 100;
            
            var italok = from i in cikkek
                         where i.FogyasztasiCikkTipus == FogyasztasiCikkTipus.Ital
                         orderby i.FogyasztasiCikkNev
                         select i;

            this.dataGrid2.Columns.Clear();
            this.dataGrid2.Items.Clear();

            this.dataGrid2.IsReadOnly = true;

            foreach (FogyasztasiCikk pia in italok)
            {
                this.dataGrid2.Items.Add(new tetelek() { tetel = pia.FogyasztasiCikkNev, ar = "" + pia.FogyasztasiCikkAr.ToString() + " Ft" });
            }

            DataGridTextColumn col3 = new DataGridTextColumn();
            col3.Binding = new Binding("tetel");

            DataGridTextColumn col4 = new DataGridTextColumn();
            col4.Binding = new Binding("ar");

            dataGrid2.Columns.Add(col3);
            dataGrid2.Columns.Add(col4);

            col3.Header = "Tétel";
            col3.Width = 200;
            col4.Header = "Ár";
            col4.Width = 100;

            DataGridTextColumn col5 = new DataGridTextColumn();
            col5.Binding = new Binding("tetel");

            DataGridTextColumn col6 = new DataGridTextColumn();
            col6.Binding = new Binding("ar");

            dataGrid3.Columns.Add(col5);
            dataGrid3.Columns.Add(col6);

            this.dataGrid3.IsReadOnly = true;

            col5.Header = "Tétel";
            col5.Width = 200;
            col6.Header = "Ár";
            col6.Width = 100;
        }

        private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            tetelek t = new tetelek();
            t=(tetelek)e.AddedItems[0];
            textBox1.Text = t.tetel;
            textBox2.Text = t.ar;
        }

        private void dataGrid2_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            tetelek t = new tetelek();
            t = (tetelek)e.AddedItems[0];
            textBox1.Text = t.tetel;
            textBox2.Text = t.ar;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            tetelek t= new tetelek();
            t.tetel=textBox1.Text;
            t.ar=textBox2.Text;
            var tetel = from i in cikkek
                         where i.FogyasztasiCikkNev== t.tetel
                         orderby i.FogyasztasiCikkNev
                         select i;

            foreach (FogyasztasiCikk tet in tetel)
            {
                rendelt.Add(tet);
            }
            this.dataGrid3.Items.Add(t);
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            

            List<Arak> arak = new List<Arak>();
            arak.AddRange(asztal.AsztalTetelek);
            for (int i = 0; i < rendelt.Count; i++)
            {
                arak.Add(new Arak(rendelt.ElementAt(i).FogyasztasiCikkNev, (int)(rendelt.ElementAt(i).FogyasztasiCikkAr), 0));
            }
            asztal.AsztalTetelek = arak.ToArray();
            this.Close();

        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            etterem.setMenuChanges();
        }
    }
}
